﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class SysApp
    {
        private readonly KeRui.DAL.SysApp dal = new KeRui.DAL.SysApp();

        public int Add(KeRui.Model.SysApp model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.SysApp> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.SysApp> list = new List<KeRui.Model.SysApp>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.SysApp item = new KeRui.Model.SysApp();
                    if (dt.Rows[i]["AppID"].ToString() != "")
                    {
                        item.AppID = int.Parse(dt.Rows[i]["AppID"].ToString());
                    }
                    item.AppName = dt.Rows[i]["AppName"].ToString();
                    if (dt.Rows[i]["ModelID"].ToString() != "")
                    {
                        item.ModelID = int.Parse(dt.Rows[i]["ModelID"].ToString());
                    }
                    if (dt.Rows[i]["IsSystem"].ToString() != "")
                    {
                        item.IsSystem = int.Parse(dt.Rows[i]["IsSystem"].ToString());
                    }
                    item.AppUrl = dt.Rows[i]["AppUrl"].ToString();
                    item.AppKey = dt.Rows[i]["AppKey"].ToString();
                    if (dt.Rows[i]["IsUsing"].ToString() != "")
                    {
                        if ((dt.Rows[i]["IsUsing"].ToString() == "1") || (dt.Rows[i]["IsUsing"].ToString().ToLower() == "true"))
                        {
                            item.IsUsing = true;
                        }
                        else
                        {
                            item.IsUsing = false;
                        }
                    }
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public int Delete(int AppID)
        {
            return this.dal.Delete(AppID);
        }

        public static DataSet GetApp()
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("SysApp", " IsSystem=1", " AppID ", " AppID,AppName ");
        }

        public List<KeRui.Model.SysApp> GetList()
        {
            string strWhere = " appID > 0";
            string strOrder = "AppID asc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("SysApp", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public KeRui.Model.SysApp GetModel(int AppID)
        {
            return this.dal.GetModel(AppID);
        }

        public KeRui.Model.SysApp GetModelByCache(int AppID)
        {
            string cacheKey = "SysAppModel-" + AppID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(AppID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("ModelCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.SysApp) cache;
        }

        public DataSet GetSelect()
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("SysApp", "", " AppID ", " AppID,AppName");
        }

        public int Update(KeRui.Model.SysApp model)
        {
            return this.dal.Update(model);
        }
    }
}

